﻿using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using HtmlAgilityPack;
using System.Net;
using System.Web;
using System.Drawing;

namespace TULT
{
    public class ChampionSelectApi
    {
        private readonly HtmlDocument _championPage = new HtmlDocument();
        private readonly HtmlDocument _generalChampionsPage = new HtmlDocument();
        private string _selectedChamp;

        /// <summary>
        /// This function loads the selected champions page, no get function cuz its converted into an URL
        /// </summary>
        public string SetChampion
        {
            get
            {
                return _selectedChamp;
            }
            set
            {
                using (WebClient client = new WebClient())
                {
                    // fix names before converting it into an URL
                    _selectedChamp = value;
                    _championPage.Load(new StringReader(client.DownloadString(@"http://www.championselect.net/champ/" + value.Replace("'", "").Replace(" ", "-").Replace(".", ""))));
                }
            }
        }

        public string SubName
        {
            get { return _championPage.DocumentNode.SelectSingleNode("//*[@id='champinfo_title']").InnerText.Trim(); }
        }

        /// <summary>
        /// Get the latest championlist from ChampionSelect.net
        /// </summary>
        public List<string> ChampionList
        {
            get
            {
                using (WebClient client = new WebClient())
                {
                    _generalChampionsPage.Load(new StringReader(client.DownloadString(@"http://www.championselect.net/Champions")));
                    
                    HtmlNodeCollection champList = _generalChampionsPage.DocumentNode.SelectNodes("//h5");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < champList.Count; i++)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(champList[i].InnerText.Trim()));
                    }
                    return stringList;
                }
            }
        }

        /// <summary>
        /// Get the champion icon of the selected champion
        /// </summary>
        public string ChampionImageInAscii
        {
            get
            {
                string url = @"http://www.solomid.net/guide/champ/" +
                             SetChampion.Replace("'", "").Replace(" ", "").Replace(".", "").ToLower() + ".png";
                WebRequest client = WebRequest.Create(url);
                using (var response = client.GetResponse())
                using (var stream = response.GetResponseStream())
                {
                    if (stream != null)
                    {
                        var bitmap = Image.FromStream(stream) as Bitmap;
                        MemoryStream dataStream = new MemoryStream();
                        if (bitmap != null) bitmap.Save(dataStream, ImageFormat.Png);
                        byte[] b = dataStream.GetBuffer();
                        dataStream.Close();
                        return Convert.ToBase64String(b);
                    }
                    return "";
                }
            }
        }
        

        public List<string> CounterSummonerSpells
        {
            get
            {
                try
                {
                    //*[@id="summary"]/div[2]/img[1]
                    //*[@id="summary"]/div[2]/img[2]
                    //*[@id="summary"]/div[2]/img[6]
                    HtmlNodeCollection counterNodes = _championPage.DocumentNode.SelectNodes("//*[@id='summary']/div[2]/img");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < counterNodes.Count; i++)
                    {
                        if (HttpUtility.HtmlDecode(counterNodes[i].GetAttributeValue("src", "").Trim()).Contains("summoners"))
                        stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].GetAttributeValue("data-original-title", "").Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }


        public List<string> CounterItems
        {
            get
            {
                try
                {
                    //*[@id="summary"]/div[2]/img[1]
                    //*[@id="summary"]/div[2]/img[2]
                    //*[@id="summary"]/div[2]/img[6]
                    HtmlNodeCollection counterNodes = _championPage.DocumentNode.SelectNodes("//*[@id='summary']/div[2]/img");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < counterNodes.Count; i++)
                    {
                        if (HttpUtility.HtmlDecode(counterNodes[i].GetAttributeValue("src", "").Trim()).Contains("items"))
                            stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].GetAttributeValue("data-original-title", "").Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }


        public List<string> CounterMechanics
        {
            get
            {
                try
                {
                    HtmlNodeCollection counterNodes =
                        _championPage.DocumentNode.SelectNodes("//*[@id='summary']/div[1]/p/span");
                    List<string> stringList = new List<string>();
                    for (int i = 0; i < counterNodes.Count; i++)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(counterNodes[i].InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }


        //*[@id="yw0_tab_1"]/div[2]/p[1]

        public List<string> BadAgainst
        {
            get
            {
                //*[@id="accordion2"]/div[1]/a/h3
                //*[@id="accordion2"]/div[3]/a/h3
                //*[@id="accordion2"]/div[5]/a/h3
                //*[@id="accordion2"]/div[7]/a/h3

                //*[@id="showMoreCounters"]/div[1]/a/h3
                //*[@id="showMoreCounters"]/div[3]/a/h3
                //*[@id="showMoreCounters"]/div[11]/a/h3
                int j = 0;
                List<string> stringList = new List<string>();
                for (int i = 1; i < 5; i++)
                {
                    HtmlNode counterNode =
                        _championPage.DocumentNode.SelectSingleNode(String.Format(
                            "//*[@id='accordion2']/div[{0}]/a/h3", i + j));
                    stringList.Add(HttpUtility.HtmlDecode(counterNode.InnerText.Trim()));
                    j++;
                }
                j = 0;
                for (int i = 1; i < 7; i++)
                {
                    HtmlNode counterNode =
                        _championPage.DocumentNode.SelectSingleNode(String.Format(
                            "//*[@id='showMoreCounters']/div[{0}]/a/h3", i + j));
                    stringList.Add(HttpUtility.HtmlDecode(counterNode.InnerText.Trim()));
                    j++;
                }
                return stringList;
            }
        }


        public List<string> GoodAgainst
        {
            //*[@id="accordion3"]/div[1]/a/h3
            //*[@id="accordion3"]/div[7]/a/h3
            //*[@id="showMoreBadPicks"]/div[1]/a/h3
            //*[@id="showMoreBadPicks"]/div[11]/a/h3
            get
            {
                int j = 0;
                List<string> stringList = new List<string>();
                for (int i = 1; i < 5; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='accordion3']/div[" + (i + j) + "]/a/h3");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    j++;
                }
                j = 0;
                for (int i = 1; i < 7; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='showMoreBadPicks']/div[" + (i + j) + "]/a/h3");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    j++;
                }
                return stringList;
            }
        }

        public List<string> GoodWith
        {
            //*[@id="accordion4"]/div[1]/a/h3
            //*[@id="accordion4"]/div[7]/a/h3
            //*[@id="showMorePartners"]/div[1]/a/h3
            get
            {
                List<string> stringList = new List<string>();
                int j = 0;
                for (int i = 1; i < 5; i++)
                {
                    HtmlNode node = _championPage.DocumentNode.SelectSingleNode("//*[@id='accordion4']/div[" + (i + j) + "]/a/h3");
                    stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    j++;
                }
                    HtmlNode node2 = _championPage.DocumentNode.SelectSingleNode("//*[@id='showMorePartners']/div[1]/a/h3");
                    stringList.Add(HttpUtility.HtmlDecode(node2.InnerText.Trim()));
                return stringList;
            }
        }

        public List<string> GetClass
        {
            //*[@id="champion-info-left"]/div[2]/span
            //*[@id="champion-info-left"]/div[2]
            //*[@id="champion-info-left"]/div[2]/span[1]
            get
            {
                try
                {
                    List<string> stringList = new List<string>();
                    HtmlNodeCollection classType =
                        _championPage.DocumentNode.SelectNodes("//*[@id='champinfo']/div[1]/span");
                    foreach (HtmlNode node in classType)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {

                    return new List<string>();
                }

            }
        }


        public List<string> GetDamageType
        {
            //*[@id="champion-info-left"]/div[3]/span
            get
            {
                try
                {
                    List<string> stringList = new List<string>();
                    HtmlNodeCollection classType =
                        _championPage.DocumentNode.SelectNodes("//*[@id='champinfo']/div[2]/span");
                    foreach (HtmlNode node in classType)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {

                    return new List<string>();
                }

            }
        }

        public List<string> GetRole
        {
            //*[@id="champion-info-left"]/div[3]/span
            get
            {
                try
                {
                    List<string> stringList = new List<string>();
                    HtmlNodeCollection classType =
                        _championPage.DocumentNode.SelectNodes("//*[@id='champinfo']/div[3]/span");
                    foreach (HtmlNode node in classType)
                    {
                        stringList.Add(HttpUtility.HtmlDecode(node.InnerText.Trim()));
                    }
                    return stringList;
                }
                catch (Exception)
                {
                    return new List<string>();
                }
            }
        }

        public string GetHealthPoints
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Health Points:')]").ChildNodes[3].InnerText;
            }
        }

        public string GetManaOrEnergyPoints
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, ':')]").NextSibling.NextSibling.ChildNodes[3].InnerText;
            }
        }

        public string GetDamage
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Damage:')]").ChildNodes[3].InnerText;
            }
        }

        public string GetAttackRange
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Attack Range:')]").ChildNodes[3].InnerText;
            }
        }

        public string GetArmor
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Armor:')]").ChildNodes[3].InnerText;
            }
        }

        public string GetHealthRegen
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Health Points:')]").ChildNodes[7].InnerText;
            }
        }

        public string GetManaOrEnergyRegen
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, ':')]").NextSibling.NextSibling.ChildNodes[7].InnerText;
            }
        }

        public string GetAttackSpeed
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Damage:')]").ChildNodes[7].InnerText;
            }
        }

        public string GetMovementSpeed
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Attack Range:')]").ChildNodes[7].InnerText;
            }
        }

        public string GetMagicResistance
        {
            get
            {
                return _championPage.DocumentNode.SelectSingleNode("//*[contains(td, 'Armor:')]").ChildNodes[7].InnerText;
            }
        }

        
    }
}
